package com.shs.com.controller;

import com.shs.com.entity.ResponseBody;
import com.shs.com.entity.User;
import com.shs.com.service.UserService;
import com.shs.com.utils.AssembleResponseMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    Map<String, Object> stringObjectMap = null;
    AssembleResponseMsg assembleResponseMsg = new AssembleResponseMsg();

    @RequestMapping("/queryUserList")
    public ResponseBody queryUserList(@RequestBody Map<String, Object> map) {
        try{
           stringObjectMap=  userService.queryUserList(map);
            System.out.println(stringObjectMap);
           return assembleResponseMsg.success(stringObjectMap);
        }catch (Exception e){
            return assembleResponseMsg.failure(200,"111","查询失败");
        }
    }
    //用户登录
    @RequestMapping("/queryUser")
    public ResponseBody queryUser(@RequestBody Map<String, Object> map) {
        User user = userService.queryUser(map);
        HashMap<String, Object> result = new HashMap<>();
        AssembleResponseMsg assembleResponseMsg = new AssembleResponseMsg();
        if(user != null){
//            这里如果为0说明是管理员，如果为1说明是普通用户
            if (user.getMgr() == 0) {
                result.put("token","admin");
            }else {
                result.put("token","editor");
            }
            result.put("status","OK");
            return assembleResponseMsg.success(result);
        }else {
            return assembleResponseMsg.failure(200,"111","账户名或密码错误");
        }
    }
    //添加用户
    @RequestMapping("/addUser")
    public ResponseBody addUser(@RequestBody Map<String,Object> map){
        try {
            userService.addUser(map);
            return new AssembleResponseMsg().success("OK");
        }catch (Exception e){
            e.printStackTrace();
            return new AssembleResponseMsg().failure(200,"111","添加用户失败");
        }
    }
//    根据用户姓名修改用户信息
    @RequestMapping("/updateUser")
    public ResponseBody updateUser(@RequestBody Map<String,Object> map){
        try {
            userService.updateUser(map);
            return new AssembleResponseMsg().success("OK");
        }catch (Exception e){
            return new AssembleResponseMsg().failure(200,"111","更新用户信息失败");
        }
    }
    //根据用户id 修改用户信息
    @RequestMapping("/updateUserById")
    public ResponseBody updateUserById(@RequestBody Map<String,Object> map){
        try {
            userService.updateUserById(map);
            return new AssembleResponseMsg().success("OK");
        }catch (Exception e){
            return new AssembleResponseMsg().failure(200,"111","修改用户信息失败");
        }
    }

//    根据用户名删除用户
    @RequestMapping("/delUser")
    public ResponseBody deleteUser(@RequestBody Map<String,Object> map){
        try {
            userService.delUser(map);
            return new AssembleResponseMsg().success("OK");
        }catch (Exception e){
            return new AssembleResponseMsg().failure(200,"111","删除用户失败");
        }
    }
    //修改用户密码
    @RequestMapping("/updateUserPass")
    public ResponseBody updateUserPass(@RequestBody Map<String,Object> map){
        try {
            userService.updateUserPass(map);
            return new AssembleResponseMsg().success("OK");
        }catch (Exception e){
            return new AssembleResponseMsg().failure(200,"111","修改密码失败");
        }
    }
}
